package com.cn.lxz.android.aws;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.CreateBucketRequest;

import java.io.ByteArrayInputStream;
import java.util.List;

public class AwsUtils {
    public String accessKey = "";
    public String secretKey = "";
    public String hostname = "";
    private AmazonS3Client client;

    public void init(){
        ClientConfiguration opts = new ClientConfiguration();
        opts.setSignerOverride("S3SignerType");
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        client = new AmazonS3Client(credentials,opts);
        S3ClientOptions options = new S3ClientOptions();
        options.setPathStyleAccess(true);
        client.setS3ClientOptions(options);
        client.setEndpoint(hostname);
    }
    //Bucket操作--start------------------------------------------
    /**
     * 创建Bucket
     * */
    public void createBucket(){
        String bucketName = "<your-bucket-name>";
        CreateBucketRequest request = new CreateBucketRequest(bucketName);
        //默认权限是私有读写，设置成公共可读写
        request.setCannedAcl(CannedAccessControlList.PublicReadWrite);
//        request.setRegion("beijing1"); //在beijing1中创建容器
        client.createBucket(request);
    }

    /**
     * 列举Bucket
     * */
    public List<Bucket> getBucketList(){
        List<Bucket> bucketList = client.listBuckets();
        return bucketList;
    }
    /**
     * 删除Bucket
     * */
    public void deleteBucket(String bucketName){
        client.deleteBucket(bucketName);
    }


    public String getBucketLocation(String bucketName){
        return client.getBucketLocation(bucketName);
    }
    public boolean isExists(String bucketName){
        return client.doesBucketExist(bucketName);
    }
    //Bucket操作--end------------------------------------------

    //上传对象--start------------------------------------------

    /**
     * 上传字符串
     * */
    public void uploadString(String bucketName,String objectKey,String content){
        client.putObject(bucketName, objectKey, new
                ByteArrayInputStream(content.getBytes()), null);
    }
    /**
     * 上传byte数组
     * */
    public void uploadStringArr(String bucketName,String objectKey,String content){
        byte[] arr = content.getBytes();
        client.putObject(bucketName, objectKey, new ByteArrayInputStream(arr),
                null);
    }

    /**
     * 创建模拟文件夹
     * 多级目录创建最后一级即可，比如dir1/dir2/dir3/，创建dir1/dir2/dir3/即可，dir1/、
     * dir1/dir2/不需要创建；
     * */
    public void createDir(String bucketName,String dir){
        String directory = dir+"/";
        client.putObject(bucketName, directory, new ByteArrayInputStream(new byte[0]),
                null);
    }

//    public void aaa(String bucketName, String objectKey, File file){
//        client.putObject(bucketName, objectKey, file);
//    }



}
